package bubbleindex;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 *
 * @author ttrott
 */
public class InputData {
    
    /**
     * ReadValues reads an external file containing two columns separated by
     * either a tab or comma, storing each column into its own string list
     * @param CSVLocation the string with the address of the file containing the
     *         data with two columns
     * @param ColumnOne
     * @param firstLine indicates whether the file contains a header
     * @param ColumnTwo
     * @param update indicates whether the file is a previously created Bubble Index
     *        If true then the file being read has name = 'DJIA''1764'days.csv
    */
    public static void ReadValues(String CSVLocation, List<String> ColumnOne, 
            List<String> ColumnTwo, boolean firstLine, boolean update) {
        
        Scanner s = null;        
        
        try {
            s = new Scanner(new BufferedReader(new FileReader(CSVLocation)));
            String line;
            
			//check for header
            if (firstLine) {
                String waste = s.nextLine();
            }
            
            while(s.hasNext()) {                
                line = s.nextLine();
                Scanner lineScan = new Scanner(line);
                lineScan.useDelimiter(",|\t");
            /*
                    When updating... The Bubble Index files contain three
                    columns. The first column is not needed.
            */
                if (update) {
                    String waste = lineScan.next();
                    ColumnOne.add(lineScan.next());
                    ColumnTwo.add(lineScan.next());
                }
                
                else {                
                    ColumnOne.add(lineScan.next());                
                    ColumnTwo.add(lineScan.next());
                }
           } 
         
        } catch (IOException | NoSuchElementException ex) {
            System.out.println("Exception. Code 029.");
        }
        
        finally {
            if (s != null) {
                s.close();
            }
        }
    }
    
    /**
     * ConvertPrices converts the string values in DailyPriceDataList to type double and 
     * stores them as double into PriceDataArray
     * @param PriceDataArray the array storing the double values
     * @param DAILY_DATA_SIZE the size of both the DataList and PriceDataArray
     */
    public static void ConvertPrices(float[] PriceDataArray, final int DAILY_DATA_SIZE) {
        for (int i = 0; i < DAILY_DATA_SIZE; i++) {
            try {                
                PriceDataArray[i] = Float.parseFloat(BubbleIndex.DailyPriceDataList.get(i));
            } catch (NumberFormatException ex) {
                System.out.println("Number Format Exception. Code 030.");
            }
        }
    }

    
    /**
     * GetDayLengths sets the size of the window to be used when 
     * calculating The Bubble Index (#### days); The values inputted 
     * into the GUI must be in filled from top to bottom 
     * @return void
     */
    public static int GetDayLengths() {
        
        String[] rawFieldData = GUI.windowsTextField.getText().split(",");
        
        for (String rawFieldData1 : rawFieldData) {
            BubbleIndex.windowLengths.add(Integer.parseInt(rawFieldData1.trim()));
        }
        
        return BubbleIndex.windowLengths.size();
    }
    
    /**
     * checkForFile checks to see if a file exists
     * @param filePathString
     * @return boolean true if file exists
     */
    public static boolean checkForFile(String filePathString) {
        File f = new File(filePathString);
        return f.exists();
    }
}